package com.apache.client.flow;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.NameValuePair;
import org.dom4j.Document;

import com.apache.client.UctCoreClient;
import com.apache.tools.PostHttp;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * 新工作流接口调用帮助类
 */
@SuppressWarnings("all")
public class FlowUtils {

	public static String ctx;//统一从用户中心统一配置中获取

	private static String getUrlPath(int type) {
		if (ToolsUtil.isNull(ctx)) {
			ctx = UctCoreClient.getConf_Map().get("flow_server");//统一从用户中心统一配置中获取
		}
		String url = "";
		switch (type) {
		case 1:
			url = ctx + "/flow/core/flow-webservice!start.action";//流程启动Action路径(flow_start)
			break;
		case 2:
			url = ctx + "/flow/core/flow-webservice!getPicNode.action";//查看当前节点坐标Action路径(flow_getPicNode)
			break;
		case 3:
			url = ctx + "/flow/core/flow-webservice!approve.action";//流程审核Action路径(flow_approve)
			break;
		case 4:
			url = ctx + "/flow/core/flow-webservice!processInsOP.action";//操作实例的Action路径(flow_processInsOP)
			break;
		case 5:
			url = ctx + "/flow/core/flow-webservice!approveData.action";//获取个人待办任务(flow_approveData)
			break;
		case 6:
			url = ctx + "/flow/core/flow-webservice!getActiveTaskInfo.action";//获取当前活动节点信息(flow_getActiveTaskInfo)
			break;
		case 7:
			url = ctx + "/flow/core/flow-webservice!dynamicallyTransition.action";//流程动态流转(flow_dynamicallyTransition)
			break;
		case 8:
			url = ctx + "/flow/core/flow-webservice!findTasksByOptions.action";//综合查询待办任务列表(flow_findTasksByOptions)
			break;
		case 9:
			url = ctx + "/flow/core/flow-webservice!overState.action";//结束流程等待状态(flow_overState)
			break;
		case 10:
			url = ctx + "/flow/core/flow-webservice!updateMapInfo.action";//更新map信息(flow_updateMapInfo)
			break;
		}
		return url;
	}

	/**
	 * 查看待办任务
	 * @param map 参数集合
	 * @return
	 */
	public static Document approveData(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {
			throw new Exception("查看待办任务时传递参数为空");
		}
		String assignee = map.get("assignee");

		if (null != assignee && !"".equals(assignee)) {
			try {
				NameValuePair[] paras = new NameValuePair[map.size()];
				Iterator<String> it = map.keySet().iterator();

				int i = 0;
				while (it.hasNext()) {
					String key = it.next();
					if (null != key && !"".equals(key)) {
						String value = map.get(key) == null ? "" : map.get(key);
						paras[i] = new NameValuePair(key, value);
						i++;
					}

				}

				document = PostHttp.postHttpDocument(getUrlPath(5), paras);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new Exception("查看待办任务时发生未知异常");

			}
		} else {
			throw new Exception("查看待办任务时,参数assignee为空");
		}

		return document;
	}

	/**
	 * 流程审核
	 * @param map 参数集合
	 * @return
	 */
	public static Document approve(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {
			throw new Exception("流程审核时传递参数为空");
		}
		String taskId = map.get("taskId");
		String result = map.get("result");
		if (null != taskId && !"".equals(taskId)) {
			if (null != result && !"".equals(result)) {
				try {
					NameValuePair[] paras = new NameValuePair[map.size()];
					Iterator<String> it = map.keySet().iterator();

					int i = 0;
					while (it.hasNext()) {
						String key = it.next();
						if (null != key && !"".equals(key)) {
							String value = map.get(key) == null ? "" : map.get(key);
							paras[i] = new NameValuePair(key, value);
							i++;
						}
					}
					document = PostHttp.postHttpDocument(getUrlPath(3), paras);
				} catch (Exception ex) {
					ex.printStackTrace();
					throw new Exception("流程审核时发生未知异常");

				}
			} else {
				throw new Exception("审核结果,参数result未传递");
			}

		} else {
			throw new Exception("任务Id,参数taskId未传递");
		}

		return document;
	}

	/**
	 * 流程启动接口
	 * @param map 参数集合
	 * @return
	 */
	public static Document start(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {

			throw new Exception("流程启动时传递参数为空");
		}
		String definitionId = map.get("definitionId");
		String deptId = map.get("deptId");
		if (null != definitionId && !"".equals(definitionId)) {
			if (null != deptId && !"".equals(deptId)) {
				try {
					NameValuePair[] paras = new NameValuePair[map.size()];
					Iterator<String> it = map.keySet().iterator();

					int i = 0;
					while (it.hasNext()) {
						String key = it.next();
						if (null != key && !"".equals(key)) {
							String value = map.get(key) == null ? "" : map.get(key);
							paras[i] = new NameValuePair(key, value);
							i++;
						}

					}
					document = PostHttp.postHttpDocument(getUrlPath(1), paras);
				} catch (Exception ex) {
					ex.printStackTrace();
					throw new Exception("流程启动时发生未知异常");

				}
			} else {
				throw new Exception("部门Id,参数deptId未传递");
			}

		} else {
			throw new Exception("流程定义Id,参数definitionId未传递");
		}

		return document;
	}

	/**
	 * 获取当前活动节点坐标
	 * @param processInsId 流程实例Id
	 * @return
	 */
	public static Document getPicNode(String processInsId) throws Exception {
		Document document = null;
		if (null != processInsId && !"".equals(processInsId)) {
			try {
				NameValuePair[] paras = new NameValuePair[1];
				paras[0] = new NameValuePair("processInsId", processInsId);

				document = PostHttp.postHttpDocument(getUrlPath(2), paras);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new Exception("获取当前活动节点坐标时发生未知异常");
			}
		} else {
			throw new Exception("流程定义Id,参数definitionId未传递");
		}
		return document;
	}

	/**
	 * 根据实例Id操作流程实例
	 * @param processInsId 实例Id
	 * @param op 操作类型  1表示挂起  2表示恢复  3表示终止 4表示结束当前活动节点(state)等待状态
	 * @return
	 */
	public static Document processInsOP(String processInsId, String op) throws Exception {
		Document document = null;
		if (StrUtil.isNotEmpty(processInsId) && StrUtil.isNotEmpty(op)) {
			try {
				NameValuePair[] paras = new NameValuePair[2];
				paras[0] = new NameValuePair("processInsId", processInsId);
				paras[1] = new NameValuePair("op", op);

				document = PostHttp.postHttpDocument(getUrlPath(4), paras);

			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			throw new Exception("返回自处理任务结果时,参数taskId 或者result为空");
		}
		return document;
	}

	/**
	 * 获取当前活动节点信息
	 * @param processInsId 实例Id
	 * @return
	 */
	public static Document getActiveTaskInfo(String processInsId) throws Exception {
		Document document = null;

		if (null != processInsId && !"".equals(processInsId)) {
			try {
				NameValuePair[] paras = new NameValuePair[1];
				paras[0] = new NameValuePair("processInsId", processInsId);

				document = PostHttp.postHttpDocument(getUrlPath(6), paras);

			} catch (Exception ex) {
				ex.printStackTrace();
				throw new Exception("Http请求失败，或发生未知异常");
			}
		} else {
			throw new Exception("参数processInsId为空");
		}
		return document;
	}

	/**
	 * 流程动态流转
	 * @param processInsId 实例Id
	 * @param sourceNode 目标节点
	 * @return
	 */
	public static Document dynamicallyTransition(String processInsId, String sourceNode) throws Exception {
		Document document = null;

		if (null != processInsId && !"".equals(processInsId) && null != sourceNode && !"".equals(sourceNode)) {
			try {
				NameValuePair[] paras = new NameValuePair[2];
				paras[0] = new NameValuePair("processInsId", processInsId);
				paras[1] = new NameValuePair("sourceNode", sourceNode);
				document = PostHttp.postHttpDocument(getUrlPath(7), paras);

			} catch (Exception ex) {
				ex.printStackTrace();
				throw new Exception("Http请求失败，或发生未知异常");
			}
		} else {
			throw new Exception("参数processInsId或sourceNode为空");
		}
		return document;
	}

	/**
	 * 待办列表综合查询
	 * @param map 参数集合
	 * @return
	 */
	public static Document findTasksByOptions(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {
			throw new Exception("流程动态流转时传递参数为空");
		}

		try {
			NameValuePair[] paras = new NameValuePair[map.size()];
			Iterator<String> it = map.keySet().iterator();

			int i = 0;
			while (it.hasNext()) {
				String key = it.next();
				if (null != key && !"".equals(key)) {
					String value = map.get(key) == null ? "" : map.get(key);
					paras[i] = new NameValuePair(key, value);
					i++;
				}
			}
			document = PostHttp.postHttpDocument(getUrlPath(8), paras);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new Exception("Http请求失败，或发生未知异常");
		}
		return document;
	}

	/**
	 * 结束流程等待状态
	 */
	public static Document overState(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {
			throw new Exception("结束流程等待状态时传递参数为空");
		}

		try {
			NameValuePair[] paras = new NameValuePair[map.size()];
			Iterator<String> it = map.keySet().iterator();

			int i = 0;
			while (it.hasNext()) {
				String key = it.next();
				if (null != key && !"".equals(key)) {
					String value = map.get(key) == null ? "" : map.get(key);
					paras[i] = new NameValuePair(key, value);
					i++;
				}
			}
			document = PostHttp.postHttpDocument(getUrlPath(9), paras);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new Exception("Http请求失败，或发生未知异常");
		}
		return document;
	}

	/**
	 * description:更新map信息
	 * @update 2016-1-21
	 */
	public static Document updateMapInfo(Map<String, String> map) throws Exception {
		Document document = null;
		if (null == map || map.size() == 0) {
			throw new Exception("更新map信息时传递参数为空");
		}
		try {
			NameValuePair[] paras = new NameValuePair[map.size()];
			Iterator<String> it = map.keySet().iterator();
			int i = 0;
			while (it.hasNext()) {
				String key = it.next();
				if (null != key && !"".equals(key)) {
					String value = map.get(key) == null ? "" : map.get(key);
					paras[i] = new NameValuePair(key, value);
					i++;
				}
			}
			document = PostHttp.postHttpDocument(getUrlPath(10), paras);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new Exception("Http请求失败，或发生未知异常");
		}
		return document;
	}
}
